package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SysRole;
import com.easy.common.service.IRoleService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    // 插入角色
    @PostMapping("/")
    public ResultData insert(@RequestBody SysRole sysRole) {
        SysRole data = roleService.insert(sysRole);
        return ResultData.success(data);
    }

    // 更新角色
    @PutMapping("/")
    public ResultData update(@RequestBody SysRole sysRole) {
        SysRole data = roleService.update(sysRole);
        return ResultData.success(data);
    }

    // 删除角色
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = roleService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // 根据 ID 获取角色
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysRole data = roleService.getById(id);
        return ResultData.success(data);
    }

    // 分页获取角色列表
    @GetMapping("/page")
    public ResultData getListByPage(Page page, SysRole sysRole) {
        IPage<SysRole> data = roleService.getListByPage(page, sysRole);
        return ResultData.success(data);
    }

    // 获取角色列表
    @GetMapping("/list")
    public ResultData getList(SysRole sysRole) {
        List<SysRole> data = roleService.getList(sysRole);
        return ResultData.success(data);
    }

    // 设置角色状态
    @PutMapping("/status/{id}")
    public ResultData setStatus(@PathVariable Integer id) {
        int result = roleService.setStatus(id);
        if (result >= 1) {
            return ResultData.success("200", "状态设置成功", null);
        } else {
            return ResultData.fail();
        }
    }
}